
package com.sm.shoplook.controller.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.service.SlService;
import com.sm.shoplook.service.service.SlSerService;
import com.sm.shoplook.util.FileUtil;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.service.req.SlServicePageSearchReqVo;

/**
 * @author chenpy
 * @desc 服务
 * @date 2018/8/16 16:31
 * @see
 */
@Controller
@RequestMapping("/slService")
public class SlServiceController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SlServiceController.class);
	@Autowired
	private SlSerService slSerService;
	@Autowired
	private FileUtil fileUtil;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String slServiceManger() {
		return "/service/manager";
	}

	/**
	 * @param slServicePageSearchReqVo
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 关于我们
	 */
	@RequestMapping(value = "/selectSlServicePage", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Map<String, Object>> selectSlServicePage(SlServicePageSearchReqVo slServicePageSearchReqVo) {
		Map<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<SlService>> pageInfoApiResult = slSerService.selectSlServicePage(slServicePageSearchReqVo);
		PageInfo<SlService> pageInfo = pageInfoApiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 跳转服务保存页面
	 */
	@RequestMapping(value = "/save_service_page/{id}", method = RequestMethod.GET)
	public String addOrUpdateRolePage(@PathVariable(value = "id") Integer id, Model model) {
		LOGGER.info("id: {}", id);
		ApiResult<SlService> slServiceApiResult = slSerService.selectSlServiceById(id);
		SlService slService = slServiceApiResult.getData();
		if (slService == null) {
			slService = new SlService();
		}
		model.addAttribute("service", slService);
		return "/service/add_update_service";
	}

	/**
	 * @param ids
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 删除服务
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteSlService", method = RequestMethod.POST)
	public ApiResult deleteSlService(String ids) {
		String[] idList = ids.split(",");
		// 批量删除
		LOGGER.info("ids", ids);
		try {
			return slSerService.deleteSlService(idList);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @param slService
	 * @param imgUrlFile
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 保存
	 */
	@ResponseBody
	@RequestMapping(value = "/saveSlService", method = RequestMethod.POST)
	public ApiResult saveSlService(SlService slService, MultipartFile imgUrlFile) {
		LOGGER.info("slService", slService);
		try {
			String filePath = fileUtil.uploadFile(imgUrlFile);
			slService.setImgUrl(filePath);
			return slSerService.saveSlService(slService);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}
}
